﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Asset
{
    public class Loader : MonoSingleton<Loader>
    {
        [Header("每帧bundle加载数量")]
        public int frameLoadBundleCount = 10;
        [Header("每帧asset加载数量")]
        public int frameLoadAssetCount = 10;
        [Header("每帧实例化数量")]
        public int frameInsObjCount = 10;
        [Header("清理Bundle的间隔 单位:秒")]
        public int clearTimeSec = 60;
        [SerializeField]
        private float clearTime = 0;

        [Header("bundle等待数量")]
        public int bundleWaitingCount = 0;
        [Header("bundle加载中数量")]
        public int bundleRunningCount = 0;
        [Header("asset等待数量")]
        public int assetWaitingCount = 0;
        [Header("asset加载中数量")]
        public int assetRunningCount = 0;

        public void init()
        {

        }

        private void Update()
        {
            clear();
            updateBundle();
            updateAsset();
            updateObj();
        }

        private List<IAsyncBundle> removeLst = new List<IAsyncBundle>();
        private void clear()
        {
            clearTime += Time.deltaTime;
            if (clearTime >= clearTimeSec)
            {
                removeLst.Clear();
                clearTime = 0;
                var ier = ResPool.Instance.getBundleEator();
                while (ier.MoveNext())
                {
                    if (ier.Current.Value.canUnload)
                    {
                        removeLst.Add(ier.Current.Value);
                    }
                }
                for (int i = 0; i < removeLst.Count; i++)
                {
                    ResPool.Instance.unloadBunle(removeLst[i]);
                }
            }
        }

        void updateObj()
        {
            var ier = ResPool.Instance.getPoolEator();
            int leftCount = this.frameInsObjCount;
            while (leftCount > 0 && ier.MoveNext())
            {
                int count = ier.Current.Value.tick(leftCount);
                leftCount -= count;
            }
        }

        void updateAsset()
        {
            //加载asset
            List<IAsyncAsset> lst = ResPool.Instance.assetRuningLst;
            for (var i = 0; i < lst.Count; ++i)
            {
                var item = lst[i];
                if (item.Status != AsyncStatus.Finish)
                {
                    this.StartCoroutine(item.doLoad());
                    continue;
                }
                item.Bundle.removeAsyncAsset(item.Name);
                lst.RemoveAt(i);
                --i;
            }
#if UNITY_EDITOR 
            assetRunningCount = lst.Count;
#endif
            int leftCount = frameLoadAssetCount - lst.Count;
            lst = ResPool.Instance.assetWaitingLst;
            for (int i = 0; i < lst.Count; i++)
            {
                if (i < leftCount)
                {
                    var item = lst[i];
                    ResPool.Instance.assetRuningLst.Add(item);
                    lst.RemoveAt(i);
                    --i;
                }
            }
#if UNITY_EDITOR 
            assetWaitingCount = lst.Count;
#endif
        }

        void updateBundle()
        {
            //加载asset
            List<IAsyncBundle> lst = ResPool.Instance.bundleRuningLst;
            for (var i = 0; i < lst.Count; ++i)
            {
                var item = lst[i];
                if (item.Status != AsyncStatus.Finish)
                {
                    this.StartCoroutine(item.doLoad());
                    continue;
                }
                lst.RemoveAt(i);
                --i;
            }
#if UNITY_EDITOR 
            bundleRunningCount = lst.Count;
#endif
            int leftCount = frameLoadAssetCount - lst.Count;
            lst = ResPool.Instance.bundleWaitingLst;
            for (int i = 0; i < lst.Count; i++)
            {
                if (i < leftCount)
                {
                    var item = lst[i];
                    ResPool.Instance.bundleRuningLst.Add(item);
                    lst.RemoveAt(i);
                    --i;
                }
            }
#if UNITY_EDITOR
            bundleWaitingCount = lst.Count;
#endif

        }

    }

}
